//
//  BindingOilCardTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BindingOilCardTableViewController.h"
#import "BindingPhoneTableViewCell.h"
#import "LogInPassWordFooterView.h"

@interface BindingOilCardTableViewController ()<LogInPassWordFooterDelegate,UITextFieldDelegate>

@property (nonatomic,strong)NSArray *textFieldTextArray;

@property (nonatomic,strong)NSMutableArray *textFieldMutableArray;

@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation BindingOilCardTableViewController

- (NSMutableArray *)textFieldMutableArray {
    if (!_textFieldMutableArray) {
        _textFieldMutableArray = [NSMutableArray array];
    }
    return _textFieldMutableArray;
}

- (NSArray *)textFieldTextArray {
    if (!_textFieldTextArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"BindingOilCardText" ofType:@"plist"];
        _textFieldTextArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _textFieldTextArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定油卡";
    
    self.tableView.backgroundColor = BACKGROUND_COLOR;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell) {
        cell = [[BindingPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString *imageString = [NSString stringWithFormat:@"bangdingyouka_%ld",indexPath.section];
    cell.leftImageView.image = [UIImage imageNamed:imageString];
    cell.textField.tag = indexPath.section;
    cell.textField.placeholder = self.textFieldTextArray[indexPath.section];
    [self.textFieldMutableArray addObject:cell.textField];
    
    if (indexPath.section == 0) {
        cell.textField.hidden = YES;
        cell.rightLabel.hidden = NO;
        cell.rightLabel.text = USERNAME;
    }
    
    if (indexPath.section == 1) {
        cell.textField.secureTextEntry = YES;
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.clearButtonMode = UITextFieldViewModeAlways;
        cell.textField.tag = 1;
        cell.textField.delegate = self;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return HEIGHT/5;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        footer.delegate = self;
        return footer;
    }
    return nil;
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearTextField];
    
    UITextField *passWordTextField = self.textFieldMutableArray[1];
    
    UITextField *oilCardTextField = self.textFieldMutableArray.lastObject;
    
    if (passWordTextField.text.length == 0 || oilCardTextField.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        
        return;
    }
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"userpwd":passWordTextField.text,
                             @"youka":oilCardTextField.text
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppUser/setUserYouka" completeBlock:^(id object) {
        
        [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定油卡成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
    
    NSLog(@"确认按钮");
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.001;
    } else {
        return 3;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self clearTextField];
    //    NSLog(@"%ld",self.textField.tag);
}


- (void)clearTextField {
    for (UITextField *textField in self.textFieldMutableArray) {
        [textField resignFirstResponder];
    }
}

- (void)didViewClickClearTextField {
    [self clearTextField];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self clearTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1) {
        [self.textFieldMutableArray[2] becomeFirstResponder];
    }
    
    return YES;
}
@end
